package com.ce.cases.report.logic;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ce.cases.assit.dao.po.CaseAssitCheck;
import com.ce.cases.report.dao.po.ReportTaxIllegal;
import com.ce.cases.source.dao.po.CaseSource;
import com.ce.cases.source.dao.po.CaseSourceDrawApprove;
import com.ce.cases.source.dao.po.DjNsrxx;
import com.ce.framework.datamaintain.implement.BaseDao;
import com.ce.framework.datamaintain.logic.ISQLMapper;
import com.ce.framework.util.GeneratorUUID;
import com.ce.util.DbUtil;
import com.zl.core.jdbc.DataSource.DsFactory;

public class ReportIllegalLogic implements IReportIllegalLogic {

	private BaseDao basedao;

	@SuppressWarnings("unchecked")
	public String updatePublicSign(String[] vcId){
		StringBuffer hql=new StringBuffer("From ReportTaxIllegal po where po.vcDelFlag=0");
		if(vcId!=null){
			hql.append(" and po.vcId in(");
			for(int i=0;i<vcId.length;i++){
				if(i==0){
					hql.append("'"+vcId[i]+"'");
				}else{
					hql.append(",'"+vcId[i]+"'");
				}

			}
			hql.append(")");
		}
		try {
			List<ReportTaxIllegal> reportList = (List<ReportTaxIllegal>)this.basedao.findByHql(hql.toString());
			if(!reportList.isEmpty()){
				boolean isDeal=true;
				List<ReportTaxIllegal> updateStateList=new ArrayList<ReportTaxIllegal>();
				List<CaseSource>  caseSourceList=new ArrayList<CaseSource>();
				for(ReportTaxIllegal reportPo:reportList){
					reportPo.setState(3);
					reportPo.setStateInfo("已推送征管局");
					updateStateList.add(reportPo);
				}
				this.basedao.updatePos(updateStateList);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "推送操作出现异常！";
		}
		return "";
	}

	@SuppressWarnings("unchecked")
	public String dealSign(String[] vcId){
		StringBuffer hql=new StringBuffer("From ReportTaxIllegal po where po.vcDelFlag=0");
		if(vcId!=null){
			hql.append(" and po.vcId in(");
			for(int i=0;i<vcId.length;i++){
				if(i==0){
					hql.append("'"+vcId[i]+"'");
				}else{
					hql.append(",'"+vcId[i]+"'");
				}

			}
			hql.append(")");
		}
		try {
			List<ReportTaxIllegal> reportList = (List<ReportTaxIllegal>)this.basedao.findByHql(hql.toString());
			if(!reportList.isEmpty()){
				boolean isDeal=true;
				List<ReportTaxIllegal> updateStateList=new ArrayList<ReportTaxIllegal>();
				List<CaseSource>  caseSourceList=new ArrayList<CaseSource>();
				for(ReportTaxIllegal reportPo:reportList){
					if(reportPo.getDealResult()==2 && reportPo.getState()==0){
						reportPo.setState(2);
						reportPo.setStateInfo("已签收");
						updateStateList.add(reportPo);
						//写入备选案源库
						CaseSource source=new CaseSource();
						source.setVcId(GeneratorUUID.generateUUID());
						source.setRegisterPerson(reportPo.getRegisterPerson());
						source.setRegisterDeptId(reportPo.getRegisterDeptId());
						source.setRegisterDept(reportPo.getRegisterDept());
						source.setRegisterDate(reportPo.getRegisterDate());
						/* 2015-06-10 by 刘渝洪，开始
						 * 添加代码：在受理检举案件时，将“接收日期”设置为检举信息登记日期
						 */
						source.setReceiveDate(reportPo.getRegisterDate());
						/* 2015-06-10 by 刘渝洪，结束 */
						source.setTaxpayerNumber(reportPo.getTaxpayerNumber());
						source.setInvolvedEnterprice(reportPo.getTaxEnterprice());
						source.setInspectionDeptId(reportPo.getInspectionDeptId());
						source.setInspectionDept(reportPo.getInspectionDept());
						source.setCollectionDeptId(reportPo.getCollectionDeptId());
						source.setCollectionDept(reportPo.getCollectionDept());
						source.setCaseFromNum("8");
						source.setCaseFromName("举报");
						/* 2015-05-25 by 刘渝洪，开始
						 * 添加代码：受理新添加的检举案件时，设置案源受理状态为“已受理”
						 */
						source.setVcAaccept("1");
						/* 2015-05-25 by 刘渝洪，结束 */
						source.setState(0);
						source.setIsSecret(1);
						source.setReportId(reportPo.getVcId());
						source.setVcDelFlag(0);
						caseSourceList.add(source);
					}else{
						isDeal=false;
						break;
					}
				}
				if(isDeal){
					this.basedao.updatePos(updateStateList);
					this.basedao.addPos(caseSourceList);
				}else{
					return "存在不允许签收的记录！";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "签收操作出现异常！";
		}
		return "";
	}

	public String dealDraw(String reportId,CaseSourceDrawApprove drawAppr){
		try {
			ReportTaxIllegal reportPo=(ReportTaxIllegal)this.basedao.getPo("com.ce.cases.report.dao.po.ReportTaxIllegal", reportId);
			if(reportPo.getDealResult()!=2 && reportPo.getState()==0){
				reportPo.setState(1);
				reportPo.setStateInfo("已提请");
				basedao.updatePo(reportPo);
				//写入备选案源库
				CaseSource source=new CaseSource();
				source.setVcId(GeneratorUUID.generateUUID());
				source.setRegisterPerson(reportPo.getRegisterPerson());
				source.setRegisterDeptId(reportPo.getRegisterDeptId());
				source.setRegisterDept(reportPo.getRegisterDept());
				source.setRegisterDate(reportPo.getRegisterDate());
				source.setTaxpayerNumber(reportPo.getTaxpayerNumber());
				source.setInvolvedEnterprice(reportPo.getTaxEnterprice());
				source.setInspectionDeptId(reportPo.getInspectionDeptId());
				source.setInspectionDept(reportPo.getInspectionDept());
				source.setCollectionDeptId(reportPo.getCollectionDeptId());
				source.setCollectionDept(reportPo.getCollectionDept());
				source.setCaseFromNum("8");
				source.setCaseFromName("举报");
				source.setState(0);
				source.setReportId(reportPo.getVcId());
				source.setVcDelFlag(0);
				this.basedao.addPo(source);
				//写入案源提请（移送）表
				drawAppr.setVcId(GeneratorUUID.generateUUID());
				drawAppr.setCaseId(source.getVcId());
				drawAppr.setVcDelFlag(0);
				this.basedao.addPo(drawAppr);
			}else{
				return "该检举记录不允许提请！";
			}
		}  catch (Exception e) {
			e.printStackTrace();
			return "提请操作出现异常！";
		}
		return "";
	}


	/**
	 *
	 * @describe  周路
	 * @ 时间：2017年8月7日12:27:18  修改Connection 为C3p0获取方式 \n
	 *
	 */
	public DjNsrxx getDjNsrxxRemote(String taxpayerNumber){
		System.out.println("ReportIllegalLogic.java-183行=》纳税人识别号-taxpayerNumber = " + taxpayerNumber);
		System.out.println("ReportIllegalLogic.java-184行=》当前所执行方法文件路径：ntax\\src\\com\\ce\\cases\\report\\logic\\ReportIllegalLogic.java" );
		//DsFactory dsFactory=new DsFactory("dbhxzg");
		Connection con=null;
		try
		{
			System.out.println("ReportIllegalLogic.java-187行=》开始链接：jdbc:oracle:thin:@//98.12.102.96:1521/cqsthxcx" );
			con=new DbUtil().getConnection_c3p0();
			//con=dsFactory.getConnection();
			/*Localhost 测试地址*/
			//con=DbUtil.getConnection(Class.forName("oracle.jdbc.driver.OracleDriver"), "jdbc:oracle:thin:@//localhost:1521/orcl", "system", "admin");

			/*正式环境远程-核心征管地址*/
			//con=DbUtil.getConnection(Class.forName("oracle.jdbc.driver.OracleDriver"), "jdbc:oracle:thin:@//98.12.102.96:1521/cqsthxcx", "CQGS_TC", "CQGS_winptc");

		} catch (Exception e1) {
			System.out.println("ReportIllegalLogic.java-195行=》远程数据库链接出错 \n\n\n" );
			e1.printStackTrace();
		}
		Statement st=null;
		ResultSet rs=null;
		List<DjNsrxx> ll=new ArrayList<DjNsrxx>();
		if(con!=null){
			String sql="select t.* from hx_dj.dj_nsrxx t where t.NSRSBH='"+taxpayerNumber+"'" + "AND NSRZT_DM='03' AND (KZZTDJLX_DM='1110' OR KZZTDJLX_DM='1120')";
			//String sql="select t.* from WS.USER_FPDK t where t.NSRSBH='"+taxpayerNumber+"'";
			try {
				st = con.createStatement();
				rs = st.executeQuery(sql);
				while(rs.next()){
					DjNsrxx po=new DjNsrxx();
					try {
						po.setVcId(""+rs.getString("DJXH"));
						po.setNsrsbh(rs.getString("NSRSBH"));
						po.setNsrmc(rs.getString("NSRMC"));
						po.setZgswjDm(rs.getString("ZGSWJ_DM"));
						po.setJcjdm(rs.getString("ZGSWJ_DM"));

						System.out.println("ReportIllegalLogic.java-214行=》远程数据库查询出来时的-主管税务局代码 rs.getString(ZGSWJ_DM)= ");
						System.out.println("ReportIllegalLogic.java-215行=》" + rs.getString("ZGSWJ_DM")+"\n\n");

						//po.setFddbrhfzrhyzxm(rs.getString("FDDBRHFZRHYZXM"));  //查询出来 的列名，必须有，不然会报错
						//po.setFddbrhfzrhyzsfzjhm(rs.getString("FDDBRHFZRHYZSFZJHM"));

					} catch (SQLException e) {
					}
					ll.add(po);
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				try {
					if(rs!=null)
						rs.close();
					if(st!=null)
						st.close();
					if(con!=null)
						con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
				}

			}
		}
		if(null!=ll&&ll.size()>0)
		{
			for (DjNsrxx djNsrxx : ll)
			{
				//System.out.println("ReportIllegalLogic.java-244行=》远程数据：jdbc:oracle:thin:@//98.12.102.96:1521/cqsthxcx ");
				System.out.println("ReportIllegalLogic.java-245行=》djNsrxx.nsrmc = " + djNsrxx.getNsrmc());
				//System.out.println("246.djNsrxx.vcid = " + djNsrxx.getVcId());
				//System.out.println("247.djNsrxx.nsrsbh = " + djNsrxx.getNsrsbh());
				//System.out.println("248.djNsrxx.getZgswjDm = " + djNsrxx.getZgswjDm());

				System.out.println(" \n\n\n");

			}
			return ll.get(0);
		}
		return null;


//		String sql="select * from EXPORT_TABLE t where t.NSRSBH='"+taxpayerNumber+"'";
//		List<DjNsrxx> l=DbUtil.executeSqlQuery(sql, new ISQLMapper(){
//			@Override
//			public Object fromSQLQuery(ResultSet rs, int num) {
//				DjNsrxx po=new DjNsrxx();
//				try {
//					po.setVcId(""+rs.getFloat("DJXH"));
//					po.setNsrsbh(rs.getString("NSRSBH"));
//					po.setNsrmc(rs.getString("NSRMC"));
//					po.setFddbrhfzrhyzxm(rs.getString("FDDBRHFZRHYZXM"));
//					po.setFddbrhfzrhyzsfzjhm(rs.getString("FDDBRHFZRHYZSFZJHM"));
//					po.setZgswjDm(rs.getString("ZGSWJ_DM"));
//				} catch (SQLException e) {
//				}
//				return po;
//			}
//
//		});
//		if(null!=l&&l.size()>0){
//			return l.get(0);
//		}
//		return null;
	}
/*	public DjNsrxx getDjNsrxxRemote(String taxpayerNumber){


		Connection con=null;
		try {
			con=DbUtil.getConnection(Class.forName("oracle.jdbc.driver.OracleDriver"), "jdbc:oracle:thin:@//98.12.102.73:1521/cqsthxqf", "wsbs_ysq", "wsbs_ysq");
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		Statement st=null;
		ResultSet rs=null;
		List<DjNsrxx> ll=new ArrayList<DjNsrxx>();
		if(con!=null){
			String sql="select t.* from hx_dj.dj_nsrxx t where t.NSRSBH='"+taxpayerNumber+"'";
			try {
				st = con.createStatement();
				rs = st.executeQuery(sql);

				while(rs.next()){
					DjNsrxx po=new DjNsrxx();
					try {
						po.setVcId(""+rs.getFloat("DJXH"));
						po.setNsrsbh(rs.getString("NSRSBH"));
						po.setNsrmc(rs.getString("NSRMC"));
						po.setFddbrhfzrhyzxm(rs.getString("FDDBRHFZRHYZXM"));
						po.setFddbrhfzrhyzsfzjhm(rs.getString("FDDBRHFZRHYZSFZJHM"));
						po.setZgswjDm(rs.getString("ZGSWJ_DM"));
					} catch (SQLException e) {
					}
					ll.add(po);
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				try {
					if(rs!=null)
						rs.close();
					if(st!=null)
						st.close();
					if(con!=null)
						con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
				}

			}

		}
		if(null!=ll&&ll.size()>0){
			return ll.get(0);
		}
		return null;


//		String sql="select * from EXPORT_TABLE t where t.NSRSBH='"+taxpayerNumber+"'";
//		List<DjNsrxx> l=DbUtil.executeSqlQuery(sql, new ISQLMapper(){
//			@Override
//			public Object fromSQLQuery(ResultSet rs, int num) {
//				DjNsrxx po=new DjNsrxx();
//				try {
//					po.setVcId(""+rs.getFloat("DJXH"));
//					po.setNsrsbh(rs.getString("NSRSBH"));
//					po.setNsrmc(rs.getString("NSRMC"));
//					po.setFddbrhfzrhyzxm(rs.getString("FDDBRHFZRHYZXM"));
//					po.setFddbrhfzrhyzsfzjhm(rs.getString("FDDBRHFZRHYZSFZJHM"));
//					po.setZgswjDm(rs.getString("ZGSWJ_DM"));
//				} catch (SQLException e) {
//				}
//				return po;
//			}
//
//		});
//		if(null!=l&&l.size()>0){
//			return l.get(0);
//		}
//		return null;
	}*/



	public BaseDao getBasedao() {
		return basedao;
	}

	public void setBasedao(BaseDao basedao) {
		this.basedao = basedao;
	}



}
